package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AssessmentPlan;
import com.ruoyi.system.service.IAssessmentPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工考核计划----DmyController
 * 
 * @author dmy
 * @date 2025-05-30
 */
@RestController
@RequestMapping("/system/plan")
public class AssessmentPlanController extends BaseController
{
    @Autowired
    private IAssessmentPlanService assessmentPlanService;

    /**
     * 查询员工考核计划----Dmy列表
     */
    @PreAuthorize("@ss.hasPermi('system:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssessmentPlan assessmentPlan)
    {
        startPage();
        List<AssessmentPlan> list = assessmentPlanService.selectAssessmentPlanList(assessmentPlan);
        return getDataTable(list);
    }

    /**
     * 导出员工考核计划----Dmy列表
     */
    @PreAuthorize("@ss.hasPermi('system:plan:export')")
    @Log(title = "员工考核计划----Dmy", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssessmentPlan assessmentPlan)
    {
        List<AssessmentPlan> list = assessmentPlanService.selectAssessmentPlanList(assessmentPlan);
        ExcelUtil<AssessmentPlan> util = new ExcelUtil<AssessmentPlan>(AssessmentPlan.class);
        util.exportExcel(response, list, "员工考核计划----Dmy数据");
    }

    /**
     * 获取员工考核计划----Dmy详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:plan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(assessmentPlanService.selectAssessmentPlanById(id));
    }

    /**
     * 新增员工考核计划----Dmy
     */
    @PreAuthorize("@ss.hasPermi('system:plan:add')")
    @Log(title = "员工考核计划----Dmy", businessType = BusinessType.INSERT)
    @PostMapping
    public int add(@RequestBody AssessmentPlan assessmentPlan)
    {
        return assessmentPlanService.insertAssessmentPlan(assessmentPlan);
    }

    /**
     * 修改员工考核计划----Dmy
     */
    @PreAuthorize("@ss.hasPermi('system:plan:edit')")
    @Log(title = "员工考核计划----Dmy", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssessmentPlan assessmentPlan)
    {
        return toAjax(assessmentPlanService.updateAssessmentPlan(assessmentPlan));
    }

    /**
     * 删除员工考核计划----Dmy
     */
    @PreAuthorize("@ss.hasPermi('system:plan:remove')")
    @Log(title = "员工考核计划----Dmy", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(assessmentPlanService.deleteAssessmentPlanByIds(ids));
    }

    @PutMapping("/add")
    public AjaxResult add(@Param("id") Integer id)
    {
        //查询员工id
        List<Integer> integers = assessmentPlanService.selectALLEmployeeIdByAssessmentPlanId(id);
        System.out.println(integers);
//        查询指标id
        List<Integer> integers1 = assessmentPlanService.selectALLIndicatorsIdByAssessmentPlanId(id);
        System.out.println(integers1);
        for (Integer integer : integers) {
            assessmentPlanService.add(integer,id);
        }
        for(Integer integer : integers){
            for (Integer integer1 : integers1) {
                assessmentPlanService.addEmployeeIndicator(integer,integer1);
            }
        }
        return toAjax(integers.size());
    }
    @PutMapping("/delete")
    public AjaxResult delete(@Param("id") Integer id) {
        //查询员工id
        List<Integer> integers = assessmentPlanService.selectALLEmployeeIdByAssessmentPlanId(id);
        System.out.println(integers);
        for (Integer integer : integers) {
            assessmentPlanService.deleteEmployeeAssessmentDetailByEmployeeId(integer);
            assessmentPlanService.deleteEmployeeIndicatorScoreByEmployeeId(integer);
        }
        return toAjax(integers.size());
    }
}
